//
//  TORadioDetailViewController.m
//  B段项目
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TORadioDetailViewController.h"
#import "TORadioDetailCell.h"
#import "TORadioModel.h"
#import "TOTools.h"
#import "TOradioDetailHeader.h"

@interface TORadioDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property(nonatomic,strong)UITableView *radioListTableView;
@property(nonatomic,strong)NSMutableArray *radioDetailListArray;
@property(nonatomic,strong)NSMutableArray *allListArray;
@property(nonatomic,assign)NSInteger nextPage;
@property(nonatomic,strong)TOradioDetailHeader *detailHeader;

@end

@implementation TORadioDetailViewController

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([ZZCMusicPlayerViewController shareMusicPlayerViewController].allRadioSelected) {
        [[ZZCMusicPlayerViewController shareMusicPlayerViewController] setAllRadioSelected:NO];

        [self removeBtns];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = self.titleStr;
    [self createTableView];
    
}

#pragma mark - 创建tableView

-(void)createTableView
{
    self.radioListTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.radioListTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.radioListTableView];
    self.radioListTableView.dataSource = self;
    self.radioListTableView.delegate = self;
    
    [self.radioListTableView registerClass:[TORadioDetailCell class] forCellReuseIdentifier:@"TORadioDetailCell"];
    
    MJRefreshGifHeader *header = [TOTools createTableViewGifHeaderWithTarget:self Action:@selector(getData)];
    
    self.radioListTableView.mj_header = header;
    [self.radioListTableView.mj_header beginRefreshing];
    
#pragma mark - 下拉刷新
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getAFNetWorkingDataIsUpLoad)];
    self.radioListTableView.mj_footer = footer;
    self.nextPage = 10;
   
#pragma mark - 创建头视图
    
    self.detailHeader = [[TOradioDetailHeader alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2.5)];
    self.radioListTableView.tableHeaderView = self.detailHeader;
}

#pragma mark - tableview协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.radioDetailListArray.count;
}

#pragma mark tableView创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TORadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TORadioDetailCell"];
    
    TORadioModel *model = self.radioDetailListArray[indexPath.row];
    
    cell.radioModel = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT/8;
}

#pragma mark - cell点击方法跳转到音乐
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"RadioPush" forKey:@"from"];
    
    TORadioModel *radioM = self.radioDetailListArray[indexPath.row];
    
    [ZZCMusicPlayerViewController shareMusicPlayerViewController].ting_id = radioM.tingid;
    [ZZCMusicPlayerViewController shareMusicPlayerViewController].radioArray = self.allListArray;

    if ([[ZZCMusicPlayerViewController shareMusicPlayerViewController] pushed]) {
        [self.navigationController popToViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
    }
    else{
        [self.navigationController pushViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
        [[ZZCMusicPlayerViewController shareMusicPlayerViewController] setPushed:YES];
    }
}

#pragma mark - 数据请求
-(void)getData
{
//    NSLog(@"%@", _radioId);
    _allListArray = [NSMutableArray array];
    _radioDetailListArray = [NSMutableArray array];
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_detail";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&radioid=%@&version=3.0.6", self.radioId];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
//        NSLog(@"%@", result);
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        self.allListArray = array;
        for (NSMutableDictionary *dic in array) {
            TORadioModel *radioDL = [[TORadioModel alloc] init];
            [radioDL setValuesForKeysWithDictionary:dic];
            [self.radioDetailListArray addObject:radioDL];
        }
        NSMutableDictionary *infoDic = [bigDic objectForKey:@"radioInfo"];
        TORadioModel *radioInfo = [[TORadioModel alloc] init];
        [radioInfo setValuesForKeysWithDictionary:infoDic];
        self.detailHeader.radioInfo = radioInfo;
        [self.radioListTableView.mj_header endRefreshing];
        [self.radioListTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@", error);
    }];
}

#pragma mark - 上拉刷新请求数据
- (void)getAFNetWorkingDataIsUpLoad{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_detail_list";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&radioid=%@&start=%ld&version=3.0.6", self.radioId, self.nextPage];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
//        NSLog(@"%@",result);
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        NSInteger total= [[bigDic objectForKey:@"total"] integerValue];
        self.allListArray = array;
        
        for (NSMutableDictionary *dic in array) {
            TORadioModel *radioDL = [[TORadioModel alloc] init];
            [radioDL setValuesForKeysWithDictionary:dic];
            [self.radioDetailListArray addObject:radioDL];
        }
        
        if (_nextPage <= total) {
            [self.radioListTableView.mj_footer endRefreshing];
        }
        else
        {
            [self.radioListTableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        self.nextPage += 10;
        
        
        
        
        [self.radioListTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}


//-(void)viewWillDisappear:(BOOL)animated
//{
//    [self removeBtns];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
